package cc.lucien.Controller;

import cc.lucien.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author lucien-ma
 * @date 2021/9/6
 * @apiNote
 */
@Controller
@RequestMapping("/view")
@SessionAttributes(value = "user")
public class ViewHandler {

    @RequestMapping("/map")
    public String map(Map<String, Object> map) {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        return "show";
    }

    @RequestMapping("model")
    public String model(Model model) {
        User user = new User();
        user.setId(2);
        user.setName("李四");
        model.addAttribute("user", user);
        return "show";
    }

    @RequestMapping("/mav1")
    public ModelAndView modelAndView1() {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping("/mav2")
    public ModelAndView modelAndView2() {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        View view = new InternalResourceView("/show.jsp");
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/mav3")
    public ModelAndView modelAndView3() {
        ModelAndView modelAndView = new ModelAndView("show");
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/mav4")
    public ModelAndView modelAndView4() {
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view);
        User user = new User();
        user.setId(2);
        user.setName("李四");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/mav5")
    public ModelAndView modelAndView5() {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setId(3);
        user.setName("王五");
        map.put("user", user);
        ModelAndView modelAndView = new ModelAndView("show", map);
        return modelAndView;
    }

    @RequestMapping("/mav6")
    public ModelAndView modelAndView6() {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setId(4);
        user.setName("王二麻子");
        map.put("user", user);
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view, map);
        return modelAndView;
    }

    @RequestMapping("/mav7")
    public ModelAndView modelAndView7() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        ModelAndView modelAndView = new ModelAndView("show", "user", user);
        return modelAndView;
    }

    @RequestMapping("/mav8")
    public ModelAndView modelAndView8() {
        User user = new User();
        user.setId(2);
        user.setName("李四");
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view, "user", user);
        return modelAndView;
    }

    /**
     * 使用 servlet 原生 web 资源
     * @param request
     * @return
     */
    @RequestMapping("/request")
    public String request(HttpServletRequest request) {
        User user = new User();
        user.setId(23);
        user.setName("詹姆斯");
        request.setAttribute("user", user);
        return "show";
    }

    @RequestMapping("/modelAttribute")
    public String modelAttribute() {
        return "show";
    }

/*    *//**
     * 接收请求，进入其他 handler 之前会先进入此方法
     * 将业务数据进行添加，所有上面的方法只返回一个视图
     * 但是依然后 业务数据，而且优先级最高
     * @return
     *//*
    @ModelAttribute
    public User getUser() {
        User user = new User();
        user.setId(3);
        user.setName("戴维斯");
        return user;
    }

    *//**
     * 没有返回值的写法
     * 而且直接装到 model 里面的优先级更高
     * 页面显示 浓眉哥，而不是 戴维斯
     *//*
    @ModelAttribute
    public void getUser2(Model model) {
        User user = new User();
        user.setId(3);
        user.setName("浓眉哥");
        model.addAttribute("user", user);
    }*/

    @RequestMapping("/session")
    public String session(HttpSession session) {
        User user = new User();
        user.setId(33);
        user.setName("皮蓬");
        session.setAttribute("user", user);
        return "show";
    }

    @RequestMapping("/sessionAnnotation")
    public ModelAndView sessionAnnotation() {
        ModelAndView modelAndView = new ModelAndView("show");
        User user = new User();
        user.setId(5);
        user.setName("王二麻子");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

}
